package com.bytedance.article.common.launchcrash;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import com.ss.android.common.applog.AppLog;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.TimeZone;

public final class e {
    private static Context a;
    private JSONObject b;
    private volatile boolean c;
    private volatile Properties d;

    e(Context arg1) {
        a = arg1;
    }

    public final void a(JSONObject arg9, com.bytedance.article.common.launchcrash.a.a$a arg10) {
        Object v0_4;
        PackageInfo v2_1;
        DisplayMetrics v5;
        int v3_3;
        String v3_1;
        JSONObject v4;
        PackageInfo v1 = null;
        int v0 = 12;
        int v2 = -12;
        if (arg9 != null && arg9.length() > 0) {
            try {
                if (this.c) {
//                    goto label_151;
                } else {
                    this.b = new JSONObject();
                    v4 = this.b;
                    try {
                        v3_1 = e.a.getPackageName();
                        e.a.getPackageManager().getApplicationInfo(v3_1, 128);
                        v4.put("package", e.a.getPackageName());
                        PackageInfo v3_2 = e.a.getPackageManager().getPackageInfo(v3_1, 0);
                        if (v3_2.applicationInfo == null) {
//                        goto label_37;
                        } else {
                            v3_3 = v3_2.applicationInfo.labelRes;
                            if (v3_3 <= 0) {
//                        goto label_37;
                            } else {
                                v4.put("display_name", e.a.getString(v3_3));
                            }
                        }
                    } catch (Exception v3) {
                    }

                    v4.put("aid", 13);
                    v4.put("sdk_version", 1);
                    v4.put("os", "Android");
                    v4.put("os_version", Build.VERSION.RELEASE);
                    v4.put("os_api", Build.VERSION.SDK_INT);
                    v4.put("device_model", Build.MODEL);
                    v4.put("device_brand", Build.BRAND);
                    v4.put("device_manufacturer", Build.MANUFACTURER);

                    v5 = e.a.getResources().getDisplayMetrics();
                    switch (v5.densityDpi) {
                        case 120:
                            v3_1 = "ldpi";
                            break;

                        case 240:
                            v3_1 = "hdpi";
                            break;
                        case 320:
                            v3_1 = "xhdpi";
                            break;
                        default: {
                            v3_1 = "mdpi";
                            break;
                        }
                    }
                    v4.put("display_density", v3_1);
                    v4.put("resolution", v5.heightPixels + "x" + v5.widthPixels);
                    v3_1 = e.a.getResources().getConfiguration().locale.getLanguage();
                    if (!TextUtils.isEmpty(((CharSequence) v3_1))) {
                        v4.put("language", v3_1);
                    }

                    v3_3 = TimeZone.getDefault().getRawOffset() / 3600000;
                    if (v3_3 >= v2) {
                        v2 = v3_3;
                    }

                    if (v2 <= v0) {
                        v0 = v2;
                    }

                    v4.put("timezone", v0);

                    v2_1 = e.a.getPackageManager().getPackageInfo(e.a.getPackageName(), 0);

                    v4.put("app_version", e.b(e.a.getApplicationContext(), "SS_VERSION_NAME"));

                    v4.put("version_code", e.a(e.a.getApplicationContext(), "SS_VERSION_CODE"));

                    v4.put("update_version_code", e.a(e.a.getApplicationContext(), "UPDATE_VERSION_CODE"));


                    if (v2_1 == null) {
//                goto label_127;
                    } else {
                        this.b.put("manifest_version_code", v2_1.versionCode);
                    }


                    if (this.d == null) {
                        this.d = new Properties();
                    }

                    this.d.load(e.a.getApplicationContext().getAssets().open("ss.properties"));
                    if (this.d.containsKey("meta_umeng_channel")) {
                        v0_4 = this.d.get("meta_umeng_channel");
                    } else {
                        v0_4 = v1;
                    }

                    if (v0_4 == null) {
                        this.c = true;
                    } else {
                        v4.put("channel", v0_4);
                    }

                }
                label_150:
                this.c = true;
                label_151:
                if (arg10 != null) {
                    this.b.put("aid", 1212);
                    if (!TextUtils.isEmpty(AppLog.deviceId())) {
                        this.b.put("device_id", AppLog.deviceId());
                    }
                }

                JSONObject v0_5 = new JSONObject();
                JSONArray v1_1 = new JSONArray();
                v1_1.put(arg9);
                v0_5.put("data", v1_1);
                this.b.put("launch_did", com.bytedance.article.common.launchcrash.c.a(e.a));
                v0_5.put("header", this.b);
                HashMap v1_2 = new HashMap();
                e.a(this.b, ((Map) v1_2));
                android.support.design.a.a(e.a("http://abn.snssdk.com/collect/", ((Map) v1_2)), v0_5.toString().getBytes(), launchNetUtils$CompressType.GZIP, "application/json; charset=utf-8");
            } catch (Throwable v0_1) {
                v0_1.printStackTrace();
            }
        }
}

    private static int a(Context arg1, String arg2) {
        return ((Integer) e.b(arg1, arg2)).intValue();
    }

    private static String a(String arg2, String arg3) {
        try {
            return URLEncoder.encode(arg2, arg3);
        } catch (UnsupportedEncodingException v0) {
            throw new IllegalArgumentException(((Throwable) v0));
        }
    }

    private static String a(String arg5, Map arg6) {
        String v1;
        if ((TextUtils.isDigitsOnly(((CharSequence) arg5))) || (arg6.isEmpty())) {
            v1 = arg5;
        } else {
            if (arg5.indexOf("?") < 0) {
                arg5 = arg5 + "?";
            }

            String v0 = arg5.endsWith("?") ? arg5 + e.a("sdk_version", "UTF-8") + "=" + e.a("1", "UTF-8") : arg5 + "&" + e.a("sdk_version", "UTF-8") + "=" + e.a("1", "UTF-8");
            if (arg6.size() > 0) {
                Iterator v2 = arg6.entrySet().iterator();
                for (v1 = v0; true; v1 = v0) {
                    label_37:
                    if (!v2.hasNext()) {
                        return v1;
                    }

                    Object v0_1 = v2.next();
                    if (arg6.get(((Map.Entry) v0_1).getKey()) == null) {
                        v0 = v1;
                    } else if (v1.endsWith("?")) {
                        v1 = v1 + e.a(((Map.Entry) v0_1).getKey().toString(), "UTF-8") + "=" + e.a(arg6.get(((Map.Entry) v0_1).getKey()).toString(), "UTF-8");
                        v0 = v1;
                    } else {
                        v0 = v1 + "&" + e.a(((Map.Entry) v0_1).getKey().toString(), "UTF-8") + "=" + e.a(arg6.get(((Map.Entry) v0_1).getKey()).toString(), "UTF-8");
                    }
                }
            }

            v1 = v0;
        }

        return v1;
    }

    private static void a(JSONObject arg3, Map arg4) {
        if (arg3 != null && arg3.length() != 0) {
            Iterator v1 = arg3.keys();
            while (v1.hasNext()) {
                Object v0 = v1.next();
                arg4.put(v0, arg3.opt(((String) v0)));
            }
        }
    }

    private static Object b(Context arg4, String arg5) {
        Object v0 = null;
        if (arg4 != null) {
            try {
                v0 = arg4.getPackageManager().getApplicationInfo(arg4.getPackageName(), PackageManager.GET_META_DATA).metaData.get(arg5);
            } catch (Exception v1) {
            }
        }

        return v0;
    }
}

